﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Data.MainServiceData;
using CPPEI.Coolzon.Server.CacheData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    class DomainUtil
    {
        /// <summary>
        /// 通知某一个客户端切换显示方案
        /// </summary>
        public static void NotifyClientSwitchSolution(Data.MainServiceData.Request request,
            CPPEI.Coolzon.Server.CacheData.ControlDomain domain,
            CacheData.Client sender,
            string solution,
            CacheData.Client receiver)
        {
            Service.Contract.RespSwitchSolutionData responseData = new Service.Contract.RespSwitchSolutionData();
            responseData.SolutionName = solution;

            Response response = new Response();
            response.Header = new ResponseHeader();
            response.Data = SerializeUtility.SerializeToBinary(responseData);
            response.Header.Scenario = Service.Contract.RESPONSE_SCENARIO.SOLUTION;
            response.Header.Command = Service.Contract.RESPONSE_COMMAND.SOLUTION.SWITCH;
            response.Header.SendTicks = DateTime.Now.Ticks;
            response.Header.DataLength = response.Data.Length;
            response.Header.RequestHeader = request.Header;
            try
            {
                receiver.Callback.OnReceiveResponse(response);
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag("MainService", "通知用户:{0}切换域:{1}的方案为:{2}失败:{3}",
                    receiver.UserIdentity,
                    domain.Id,
                    solution,
                    ex);
                receiver.Callback = null;
            }
        }

        /// <summary>
        /// 通知其他客户端切换显示方案,
        /// 只有受控制的用户才会切换方案，不送控制的不会切换
        /// </summary>
        public static void NotifyOtherClientSwitchSolution(Data.MainServiceData.Request request,
            CPPEI.Coolzon.Server.CacheData.ControlDomain domain,
            CacheData.Client sender,
            string solution)
        {
            List<CacheData.Client> clientList = domain.GetClientList();

            // 通知各个客户端场景事件
            for (int index = 0; index < clientList.Count; index++)
            {
                CacheData.Client client = clientList[index];
                if (client == sender
                    || client.ControlState != ControlState.UnderControl)
                {
                    continue;
                }

                client.State = ClientState.SwitchSolution;

                Service.Contract.RespSwitchSolutionData responseData = new Service.Contract.RespSwitchSolutionData();
                responseData.SolutionName = solution;

                Response response = new Response();
                response.Header = new ResponseHeader();
                response.Data = SerializeUtility.SerializeToBinary(responseData);
                response.Header.Scenario = Service.Contract.RESPONSE_SCENARIO.SOLUTION;
                response.Header.Command = Service.Contract.RESPONSE_COMMAND.SOLUTION.SWITCH;
                response.Header.SendTicks = DateTime.Now.Ticks;
                response.Header.DataLength = response.Data.Length;
                response.Header.RequestHeader = request.Header;
                try
                {
                    client.Callback.OnReceiveResponse(response);
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag("MainService", "通知用户:{0}切换域:{1}的方案为:{2}失败:{3}",
                        client.UserIdentity,
                        domain.Id,
                        solution,
                        ex);
                    client.Callback = null;
                }
            }
        }

        /// <summary>
        /// 通知其他处于等待状态的展示端进入
        /// </summary>
        public static void NotifyDisplayClientEnter(Data.MainServiceData.Request request, CPPEI.Coolzon.Server.CacheData.ControlDomain domain)
        {
            CacheData.Client[] clientList = CacheData.ServerData.Instance.GetClientList();

            // 通知各个客户端场景事件
            for (int index = 0; index < clientList.Length; index++)
            {
                CacheData.Client client = clientList[index];
                if (client != null
                    && client.Callback != null
                    && client.State == ClientState.Waiting)
                {
                    domain.AddClient(client);

                    // 创建控制域时拉入到域的用户不需要同步视图状态，不需要中间状态
                    client.State = ClientState.InDomain;

                    Service.Contract.RespEnterDomainData enterDomainResponseData = new Service.Contract.RespEnterDomainData();
                    enterDomainResponseData.IsSuccess = true;
                    enterDomainResponseData.Message = "控制端拉入控制域";
                    enterDomainResponseData.SolutionName = domain.SolutionName;
                    enterDomainResponseData.SolutionCaption = domain.SolutionCaption;
                    enterDomainResponseData.DomainId = domain.Id;
                    enterDomainResponseData.DomainCaption = domain.Caption;
                    enterDomainResponseData.OperatorIdentity = domain.OperatorIdentity;
                    enterDomainResponseData.ViewStateAge = domain.ViewStateAge;
                    enterDomainResponseData.ViewStateToken = domain.ViewStateToken;
                    enterDomainResponseData.ViewStateSolution = domain.SolutionName;

                    Response response = new Response();
                    response.Header = new ResponseHeader();
                    response.Data = SerializeUtility.SerializeToBinary(enterDomainResponseData);
                    response.Header.Scenario = Service.Contract.RESPONSE_SCENARIO.CONNENT;
                    response.Header.Command = Service.Contract.RESPONSE_COMMAND.CONNECT.ENTERDOMAIN;
                    response.Header.SendTicks = DateTime.Now.Ticks;
                    response.Header.DataLength = response.Data.Length;
                    response.Header.RequestHeader = request.Header;
                    try
                    {
                        client.Callback.OnReceiveResponse(response);
                    }
                    catch (Exception ex)
                    {
                        Common.Logger.ErrorToTag("MainService", "通知用户{0}进入控制域{1}失败:{2}",
                            client.UserIdentity,
                            domain.Id,
                            ex);
                        client.Callback = null;
                    }
                }
            }
        }
    }
}
